package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbCoupon;
import com.bwie.service.TbCouponService;
import com.bwie.mapper.TbCouponMapper;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtils;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author andy
* @description 针对表【tb_coupon(优惠券表)】的数据库操作Service实现
* @createDate 2025-07-08 15:07:07
*/
@Service
public class TbCouponServiceImpl extends ServiceImpl<TbCouponMapper, TbCoupon>
    implements TbCouponService{

    @Autowired
    TbCouponMapper couponMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public R getCouponList(PageInfoVo pageInfoVo) {
        //--1 构造分页参数
        Page<TbCoupon> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        //--2 分页查询
//        QueryWrapper<TbCoupon> wrapper = new QueryWrapper<>();
//
//        // 添加查询条件
//        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())) {
//            wrapper.lambda().like(TbCoupon::getCouponName, pageInfoVo.getKeyWord());
//        }
//
//        wrapper.lambda().orderByDesc(TbCoupon::getCreateTime);
//        Page<TbCoupon> tbCouponPage = couponMapper.selectPage(page, wrapper);
        Page<TbCoupon> tbCouponPage = couponMapper.getCouponList(page, pageInfoVo);

        return R.ok().setData(tbCouponPage);
    }

    @Override
    public R addCoupon(TbCoupon tbCoupon) {
        //--1 获取token
        String token = request.getHeader("token");
        if(token == null) {
            return R.error().setMsg("请先登录");
        }
        String userName = TokenUtils.getUserName(token);

        tbCoupon.setCreateBy(userName);
        tbCoupon.setCreateTime(new Date());

        //--2 领取时间区间处理
        List<String> getTimeRange = tbCoupon.getGetTimeRange();
        if(getTimeRange != null && getTimeRange.size() == 2) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                tbCoupon.setGetBeginTime(format.parse(getTimeRange.get(0)));
                tbCoupon.setGetEndTime(format.parse(getTimeRange.get(1)));
            } catch (ParseException e) {
//                throw new RuntimeException(e);
                e.printStackTrace();
            }
        }

        //--3 使用时间区间处理
        List<String> useTimeRange = tbCoupon.getUseTimeRange();
        if(useTimeRange != null && useTimeRange.size() == 2) {
            // 使用区间
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                tbCoupon.setBeginTime(format.parse(useTimeRange.get(0)));
                tbCoupon.setEndTime(format.parse(useTimeRange.get(1)));
            } catch (ParseException e) {
                //throw new RuntimeException(e);
                e.printStackTrace();
            }
        }
        else {
            // 使用天数
            tbCoupon.setBeginTime(new Date());
            tbCoupon.setEndTime(new Date(tbCoupon.getBeginTime().getTime() + tbCoupon.getUseDay() * 24 * 60 * 60 * 1000));
        }


        //--2 更新审计字段
        couponMapper.insert(tbCoupon);

        return R.ok();
    }

    @Override
    public R copyCoupon(Integer couponId) {
        // --1 查询优惠券
        TbCoupon tbCoupon = couponMapper.selectById(couponId);
        if(tbCoupon == null) {
            return R.error().setMsg("优惠券不存在");
        }

        //--2 复制优惠券
        tbCoupon.setCouponId(null);
        tbCoupon.setCouponStatus(0);
        couponMapper.insert(tbCoupon);

        return R.ok().setMsg("复制成功");
    }
}




